package com.training.service;

import com.training.entity.Course;
import com.training.entity.PurchaseRecord;
import com.training.entity.ShoppingCart;
import com.training.mapper.PurchaseRecordMapper; // 引入 PurchaseRecordMapper
import com.training.mapper.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper; // 注入 PurchaseRecordMapper

    /**
     * Add a course to the student's shopping cart.
     * Before adding, it checks if the course is already in the cart or has been purchased.
     * @param studentId Student ID
     * @param courseId Course ID
     * @return true if added successfully, false if already in cart or purchased
     */
    @Transactional
    public boolean addCourseToCart(Long studentId, Long courseId) {
        // 1. Check if the course is already in the shopping cart (deleted = 0)
        ShoppingCart existingCartItem = shoppingCartMapper.getCartItemByStudentIdAndCourseId(studentId, courseId);
        if (existingCartItem != null) {
            return false; // Already in cart
        }

        // 2. Check if the course has already been purchased by this student (deleted = 0)
        // 使用 purchaseRecordMapper 检查购买记录
        if (purchaseRecordMapper.countPurchaseRecord(studentId, courseId) > 0) {
            return false; // Already purchased
        }

        // 3. Add to cart
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setStudentId(studentId);
        shoppingCart.setCourseId(courseId);
        shoppingCart.setDeleted(0); // Mark as not deleted
        return shoppingCartMapper.addToCart(shoppingCart) > 0;
    }

    /**
     * Get all active (non-deleted) shopping cart items for a student.
     * @param studentId Student ID
     * @return List of Course objects in the student's shopping cart
     */
    public List<Course> getCartItemsByStudentId(Long studentId) {
        return shoppingCartMapper.getCartItemsByStudentId(studentId);
    }

    /**
     * Remove a course from the student's shopping cart.
     * This now performs a physical deletion.
     * @param studentId Student ID
     * @param courseId Course ID
     * @return true if removed successfully, false otherwise
     */
    @Transactional
    public boolean removeCourseFromCart(Long studentId, Long courseId) {
        // 使用物理删除方法
        return shoppingCartMapper.physicalDeleteCartItemByStudentIdAndCourseId(studentId, courseId) > 0;
    }

    /**
     * Clear all items from a student's shopping cart.
     * This now performs a physical deletion of all items.
     * @param studentId Student ID
     * @return true if cleared successfully, false otherwise
     */
    @Transactional
    public boolean clearCartByStudentId(Long studentId) {
        // 使用物理清空购物车方法
        return shoppingCartMapper.physicalClearCartByStudentId(studentId) > 0;
    }
}